$(function () {
    $("#jqGrid").jqGrid({
        url: '../bookvideo/list4Show',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'Id', width: 50, key: true},
            {label: '单元名称', name: 'unitName', index: 'unit_name', width: 80},
            {label: '页码', name: 'pageNo', index: 'page_no', width: 80},
            {label: '视频名称', name: 'videoName', index: 'video_name', width: 80},
            {
                label: '视频地址', name: 'videoUrl', index: 'video_url', width: 80,
                formatter: function (value, options, row) {

                    let bookVideoHtml = '<div><a class="btn btn-primary" onclick="alert(' + "点击了上传方法" + ')">上传</a>';
                    if (value != null && value.trim() != "") {
                        bookVideoHtml += '<a class="btn btn-primary" href="' + value + '" target="view_window">查看</a>';
                    }
                    return bookVideoHtml += '</div>';
                }
            },
            {label: '二维码场景值', name: 'qrcodeNo', index: 'qrcode_no', width: 80},
            {
                label: '二维码地址', name: 'qrcodeUrl', index: 'qrcode_url', width: 80,
                formatter: function (value, options, row) {
                    if (value != null) {
                        let qrcodeHtml = '<a class="btn btn-primary" href="' + value + '" target="view_window">预览</a>';
                        return qrcodeHtml;
                    } else {
                        return "";
                    }
                }
            },
            {label: '描述', name: 'description', index: 'description', width: 80},
            {label: '教辅书', name: 'bookName', index: 'bookName', width: 80},
            {
                label: '年级', name: 'grade', index: 'grade', width: 80,
                formatter: function (value, options, row) {
                    if (value != null) {
                        return value.name;
                    } else {
                        return "";
                    }
                }
            },
            {
                label: '科目', name: 'subject', index: 'subject', width: 80,
                formatter: function (value, options, row) {
                    if (value != null) {
                        return value.name;
                    } else {
                        return "";
                    }
                }
            },
            {
                label: '教版', name: 'version', index: 'version', width: 80,
                formatter: function (value, options, row) {
                    if (value != null) {
                        return value.name;
                    } else {
                        return "";
                    }
                }
            },
            {
                label: '册别', name: 'fascicle', index: 'fascicle', width: 80,
                formatter: function (value, options, row) {
                    if (value != null) {
                        return value.name;
                    } else {
                        return "";
                    }
                }
            },
            {
                label: '年季', name: 'season', index: 'season', width: 80,
                formatter: function (value, options, row) {
                    if (value != null) {
                        return value.name;
                    } else {
                        return "";
                    }
                }
            },
            {label: '排序字段', name: 'orderField', index: 'order_field', width: 80},
            {label: '创建时间', name: 'createTime', index: 'create_time', width: 80},
            {label: '修改时间', name: 'updateTime', index: 'update_time', width: 80},
            {
                label: '审核状态', name: 'status', index: 'status', width: 80,
                formatter: function (value, options, row) {

                    if (value == 0) {
                        return "未审核";
                    } else if (value == 1) {
                        return "审核已通过";
                    } else {
                        return "审核未通过";
                    }
                }
            },
            {
                label: '是否删除', name: 'deleted', index: 'deleted', width: 80,
                formatter: function (value, options, row) {
                    if (value == 0) {
                        return "否";
                    } else {
                        return "是";
                    }
                }
            },
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
    vm.getDictList();
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        bookVideo: {},
        book: {
            gradeId: -1,
            seasonId: -1,
            subjectId: -1,
            versionId: -1,
            fascicleId: -1
        },
        gradeList: [],
        subjectList: [],
        versionList: [],
        fascicleList: [],
        seasonList: [],
        key: null,
        status: -1
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.bookVideo = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.bookVideo.id == null ? "../bookvideo/save" : "../bookvideo/update";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.bookVideo),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../bookvideo/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getDictList: function () {
            $.get("../dictionary/list4Show", function (r) {
                vm.gradeList = r.dictionaryList.filter(dirctionary => dirctionary.type === "T01");
                vm.subjectList = r.dictionaryList.filter(dirctionary => dirctionary.type === "T02");
                vm.versionList = r.dictionaryList.filter(dirctionary => dirctionary.type === "T03");
                vm.fascicleList = r.dictionaryList.filter(dirctionary => dirctionary.type === "T04");
                vm.seasonList = r.dictionaryList.filter(dirctionary => dirctionary.type === "T05");
                vm.gradeList.unshift({id: -1, name: "选择年级"});
                vm.subjectList.unshift({id: -1, name: "选择科目"});
                vm.versionList.unshift({id: -1, name: "选择教版"});
                vm.fascicleList.unshift({id: -1, name: "选择册别"});
                vm.seasonList.unshift({id: -1, name: "选择年季"});
            });
        },
        getInfo: function (id) {
            $.get("../bookvideo/info/" + id, function (r) {
                vm.bookVideo = r.bookVideo;
            });
        },
        clearJqGrid: function () {
            var postData = $('#jqGrid').jqGrid("getGridParam", "postData");
            $.each(postData, function (k, v) {
                delete postData[k];
            });
        },
        publish: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            confirm('确定要审核选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../bookvideo/publish",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        remove: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            confirm('确定要反审核选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../bookvideo/remove",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },

        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            //查询时需要携带请求参数
            let postdata = '{ "a":"a"';
            if (vm.book.name != null && vm.book.name != undefined) {
                postdata += ',"bookName": "'+vm.book.name+'"';
            }
            if (vm.book.gradeId != "-1" && vm.book.gradeId != undefined) {
                postdata += ',"gradeId": ' + vm.book.gradeId
            }
            if (vm.book.subjectId != "-1" && vm.book.subjectId != undefined) {
                postdata += ',"subjectId": ' + vm.book.subjectId
            }
            if (vm.book.versionId != "-1" && vm.book.versionId != undefined) {
                postdata += ',"versionId": ' + vm.book.versionId
            }
            if (vm.book.fascicleId != "-1" && vm.book.fascicleId != undefined) {
                postdata += ',"fascicleId": ' + vm.book.fascicleId
            }
            if (vm.book.seasonId != "-1" && vm.book.seasonId != undefined) {
                postdata += ',"seasonId": ' + vm.book.seasonId
            }
            if (vm.status != "-1" && vm.status != undefined) {
                postdata += ',"status": ' + vm.status
            }
            if (vm.key != null && vm.status != undefined) {
                postdata += ',"key": "' + vm.key+'"';
            }
            postdata += "}";
            vm.clearJqGrid();
            $("#jqGrid").jqGrid('setGridParam', {
                postData: $.parseJSON(postdata),
                page: page
            }).trigger("reloadGrid");
        }
    }
});